﻿using System;
using System.Collections.Generic;

namespace QSharp.Scheme.Utility.HBSpaceMgmt
{
    using Classical.Sequential;


    public class Hole : IComparable<Hole>
    {
        public IPosition Start = default(IPosition);
        public ISize Size = default(ISize);

        public Hole(IPosition start, ISize size)
        {
            Start = start;
            Size = size;
        }

        public int CompareTo(Hole other)
        {
            int c = this.Size.CompareTo(other.Size);
            if (c != 0)
                return c;
            c = this.Start.CompareTo(other.Start);
            return c;
        }

        protected static int Compare(Hole hole, ISize size)
        {
            return hole.Size.CompareTo(size);
        }

        protected class HoleSizeComparer : IComparable<Hole>
        {
            public readonly ISize Size;

            public HoleSizeComparer(ISize size)
            {
                Size = size;
            }

            public int CompareTo(Hole t)
            {
                return Size.CompareTo(t.Size);
            }
        }
        /**
         * <summary>
         *  returns the the position of the first hole in 
         *  the list that can offer a space of specific size,
         *  it is not guaranteed to be an insertion position 
         *  for a hole with size specified by `size', one should
         *  use in-built binary-search of the list instead
         *  
         *  If the returned index exceeds the range of the list,
         *  the list doesn't contain a hole that can offer the
         *  space required
         * </summary>
         */
        public static bool Search(IList<Hole> holes, ISize size, out int index)
        {
            return BinarySearch.Search<Hole>(holes, new HoleSizeComparer(size), out index);
        }

    }
}
